import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/model/message.dart';
import 'package:meal/route/route.dart';
import 'package:meal/ui/scroll_refresh_config.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class SystemList extends StatefulWidget {
  const SystemList({Key? key}) : super(key: key);

  @override
  State<SystemList> createState() => _SystemListState();
}

class _SystemListState extends State<SystemList> {
  final RefreshController refreshController = RefreshController(initialRefresh: false);

  final List<MessageModel> pageList = [];

  @override
  void initState() {
    super.initState();
    pageList.add(MessageModel(
        title: '店铺LOGO审核通过', content: '您修改的店铺LOGO审核未通过。', addTime: '2023-05-11 12:24:13'));
    pageList.add(MessageModel(
        title: '店铺LOGO审核通过', content: '您修改的店铺LOGO审核未通过。', addTime: '2023-05-11 12:24:13'));
    pageList.add(MessageModel(
        title: '店铺LOGO审核通过', content: '您修改的店铺LOGO审核未通过。', addTime: '2023-05-11 12:24:13'));
    pageList.add(MessageModel(
        title: '店铺LOGO审核通过', content: '您修改的店铺LOGO审核未通过。', addTime: '2023-05-11 12:24:13'));
  }

  @override
  void dispose() {
    refreshController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ToolsUtils.getDefaultBackgroundColor(),
      appBar: ToolsUtils.getDefaultBrnAppBar('系统通知'),
      body: ScrollRefreshConfig(
        refreshController: refreshController,
        onRefresh: onRefresh,
        onLoading: onLoading,
        child: listViewBuilder(),
      ),
    );
  }

  Widget listViewBuilder() {
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        var item = pageList[index];
        return InkWell(
          onTap: () {
            Get.toNamed(RouteGet.userStoreInfo);
          },
          child: Container(
            margin: const EdgeInsets.only(
                top: Sizes.default20, left: Sizes.default20, right: Sizes.default20),
            child: Column(
              children: [
                Container(
                  alignment: Alignment.center,
                  padding: const EdgeInsets.only(bottom: Sizes.default20),
                  child: Text(item.addTime,
                      style:
                      StylesUtils.customTextStyle(color: '#A1A1A1', fontSize: Sizes.default13)),
                ),
                Container(
                  decoration: BoxDecoration(
                      color: Colors.white, borderRadius: BorderRadius.circular(Sizes.default10)),
                  padding: const EdgeInsets.symmetric(vertical: Sizes.default15, horizontal: Sizes.default20),
                  child: ListBody(
                    children: [
                      Container(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          item.title,
                          style: StylesUtils.customTextStyle(
                              fontSize: Sizes.default15, color: ColorsUtils.color363),
                        ),
                      ),
                      Container(
                        margin: const EdgeInsets.only(top: Sizes.default5, bottom: Sizes.default15),
                        alignment: Alignment.centerLeft,
                        child: Text(
                          item.content ?? '',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color797, fontSize: Sizes.default13),
                        ),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text('查看详情',
                              style: StylesUtils.customTextStyle(
                                  fontSize: Sizes.default12, color: ColorsUtils.color797)),
                          BrunoTools.getAssetSizeImage(
                              BrnAsset.iconRightArrow, Sizes.default16, Sizes.default16)
                        ],
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
        );
      },
      itemCount: pageList.length,
      itemExtent: 160,
    );
  }

  void onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
    setState(() {});
  }

  void onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 50) {
      return refreshController.loadNoData();
    }

    /// 添加订单
    refreshController.loadComplete();
    pageList.add(MessageModel(
        title: '店铺LOGO审核通过', content: '您修改的店铺LOGO审核未通过。', addTime: '2023-05-11 12:24:13'));
    setState(() {});
  }
}
